﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerConllter : MonoBehaviour, IDamageble
{

    private Rigidbody2D rb;

    private Animator anim;

    public float speed;
    public float jumpForce;

    public int jumpNumber = 0;

    [Header("Player State")]
    public float health;
    public bool isDead;

    public float attack = 1;

    [Header("Ground Check")]
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask groundLayer;

    public Collider2D skillColl;

    [Header("States Check")]
    public bool isGround;
    public bool canJump;
    public bool isJump;
    public bool isDoorInside;

    [Header("Effect")]
    public GameObject jumpEffect;
    public GameObject FallEffect;

    [Header("Attack Settings")]
    public GameObject bombPrefab;
    public float nextAttack = 0;  //
    public float attackRate;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        health = GameManager.instance.LoadHealth();
        UIManager.instance.UpdateHealth(health);
    }

    // Update is called once per frame
    void Update()
    {
        if (isDead)
        {
            anim.SetBool("dead", isDead);
            return;
        }
        CheckInput();
    }
    public void FixedUpdate()
    {
        if (isDead)
        {
            rb.velocity = Vector2.zero;
            return;
        }
        PhysicsCheck();
        Movement();
    }
    void CheckInput()
    {
        if (Input.GetButtonDown("Jump") && jumpNumber < 3)
        {
            Jump();
        }

        if (Input.GetKeyDown(KeyCode.J))
        {
            Attack();
        }
        if (Input.GetKeyDown(KeyCode.I))
        {
            KeyDownI();
        }
        if (Input.GetKeyDown(KeyCode.K))
        {
            KeyDownK();
        }
    }
    public void Attack()
    {
        if (Time.time > nextAttack)
        {
            //生成预制体
            var bomb = Instantiate(bombPrefab, transform.position, bombPrefab.transform.rotation);

            bomb.GetComponent<Rigidbody2D>().AddForce(new Vector2(transform.localScale.x, 0.1f) * 5, ForceMode2D.Impulse);
            print(bomb.GetComponent<Rigidbody2D>().velocity);
            nextAttack = Time.time + attackRate;
        }
    }
    //左右移动
    void Movement()
    {
        // float horizontalInput = Input.GetAxis("Horizontal"); //~1 - 1 包含小数
        float horizontalInput = Input.GetAxisRaw("Horizontal"); //~1 - 1 按下a键 -1， 按下d键 1
        rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
        if (horizontalInput != 0)
        {
            transform.localScale = new Vector3(horizontalInput, 1, 1);
        }
    }

    public void KeyDownK()
    {
        anim.SetTrigger("attack");
    }
    //跳跃
    void Jump()
    {
        if (jumpNumber < 1)
        {
            isJump = true;
            FallEffect.SetActive(false);
            jumpEffect.SetActive(true);
            jumpEffect.transform.position = transform.position + new Vector3(0, -0.45f, 0);
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            jumpNumber++;
        }
    }
    //按下i 前往下一关
    public void KeyDownI()
    {
        if (isDoorInside)
        {
            GameManager.instance.NextScene();
        }
    }


    //检测是否在地上
    void PhysicsCheck()
    {
        //OverlapCircle 检测半径内指定图层的单个对象
        isGround = Physics2D.OverlapCircle(groundCheck.position, checkRadius, groundLayer);
        if (isGround)
        {
            isJump = false;
            rb.gravityScale = 1;
            jumpNumber = 0;
        }
        else
        {
            isJump = false;
            rb.gravityScale = 4;
        }
    }

    //在界面上画图形
    //Unity自动调用，只在预览时才会调用
    public void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(groundCheck.position, checkRadius);
    }

    //落地式执行落地动画
    public void HandFallEffect()
    {
        FallEffect.transform.position = transform.position + new Vector3(0, -0.75f, 0);
        FallEffect.SetActive(true);
    }


    public void GetHit(float damage)
    {
        //判断正在播放的动画是受伤，那么就不扣血
        if (!anim.GetCurrentAnimatorStateInfo(1).IsName("hit"))
        {
            health -= damage;
            if (health < 1)
            {
                health = 0;
                isDead = true;
            }
            anim.SetTrigger("hit");
            UIManager.instance.UpdateHealth(health);
            PlayerPrefs.SetFloat("playerHealth", health);
        }
    }

}
